<?php
namespace app\member\controller;

use app\common\controller\MemberBase;
use app\common\model\UserMoney as cxModel;
use think\Loader;

class UserMoney extends MemberBase {

    //  会员中心首页
    public function index(){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $money_class = '1';
        $map['uid'] = $this->cxbsuser['uid'];
        $dmap['uid'] = $this->cxbsuser['uid'];
        $moneytitle = "1";
        if($this->cxbsuser['group_id'] == '6'){
            $money_class = '0';
            $map['addcont'] = array('in','3,4');
            $dmap['addcont'] = '10';
            $moneytitle = "2";
            if(!empty($getdata['cont'])){
                $moneytitle = "3";
                $map['addcont'] = $getdata['cont'];
                $dmap['addcont'] = '5';
            }
        }
        $map['money_class'] = $money_class;
        $dmap['money_class'] = $money_class;
        $moneylist = $cxmodel->where($map)->order('addtime desc')->paginate('20');
        $money = $cxmodel->where($map)->order('id desc')->find();
        if(empty($money)){
            $money['paymoney'] = $money['user_money'] = $money['dmoney'] = $money['emoney'] = $money['dxmoney'] = $money['exmoney'] = $money['all_money'] = '0';
        }
        //  查询7天前的收入
        $parttime['zhouadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-7 day")));
        $dmoney = $cxmodel->where('uid',$map['uid'])->where('money_class',$money_class)->where('addcont','in','3,4')->where('addtime','>',$parttime['zhouadd'])->where('addtime','<',time())->sum('money');
        //  查询冻结余额
        $putmoney = $cxmodel->where($dmap)->sum('money');
        $webs = array(
            'title' => '我的推广---'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'webs' => $webs,
            'list' => $moneylist,
            'money' => $money,
            'dmoney' => $dmoney,
            'moneytitle' => $moneytitle,
            'putmoney' => $putmoney,
        ]);
        return view($this->temp.'user_money.htm');
    }
    //  用户充值
    public function addmoney(){
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $mid = '1003';
            if(!empty($data['mid']) && $data['mid'] == '1004'){
                //  查询订单是否存在
                $oldorder = model('Order')->where('mid','1004')->where('uid',$this->cxbsuser['uid'])->where('playstatus','in','0,4')->find();
                if(!empty($oldorder)){
                    $this->success('处理成功，去支付！',url('home/Order/playOrder',array('oid'=>$oldorder['oid'])));
                }
                $data['money'] = $this->webdb['baozhengjin'];
                $mid = $data['mid'];
            }
            $validate = Loader::validate("UserMoney");
            if (!$validate->scene('addmoney')->check($data)) {
                $this->error($validate->getError());
            }
            //  开始构建订单
            //  获取用户地址
            $shoptitle = "用户充值";
            if($mid == '1003'){
                $address = model('UserAddress')->where('uid',$this->cxbsuser['uid'])->order('id desc')->find();
                if(empty($address)){
                    $this->error('请先添加地址','UserAddress/index');
                }
                $address = $address->toArray();
            }
            if($mid == '1004'){
                $address = array(
                    'adduname' => $this->cxbsuser['uname'],
                    'phone' => $this->cxbsuser['username'],
                    'addres' => $this->cxbsuser['uaddress'],
                );
                $shoptitle = "缴纳保证金";
            }
            $oid = 'CYE'.date("Ymd").$this->addoid();
            $user = array(
                'uname' => $address['adduname'],
                'uphone' => $address['phone'],
                'uaddress' => $address['addres'],
            );
            $shop = array(
                'num' => '1',
                'aid' => '1',
                'title' => $shoptitle,
                'picurl' => "",
                'money' => $data['money'],
                'oid' => $oid,
                'uid' => $this->cxbsuser['uid'],
            );
            $neworder = array(
                'oid' => $shop['oid'],
                'uid' => $shop['uid'],
                'aid' => $shop['aid'],
                'mid' => $mid,
                'addtime' => time(),
                'title' => $shop['title'],
                'uphone' => $user['uphone'],
                'allmoney' => $shop['money'] * 100,
                'pricemoney' => $shop['money'] * 100,
                'playstatus' => '0',
                'class' => '0',
            );
            $neworder['conf']['user'] = $user;
            $neworder['conf']['shop'] = $shop;
            $neworder['conf'] = serialize($neworder['conf']);
            if(model('Order')->add_chong($neworder)){
                $this->success('处理成功，去支付！',url('home/Order/playOrder',array('oid'=>$neworder['oid'])));
            }else{
                $this->error('提交失败，请刷新重试！');
            }
        }
        return view($this->temp.'user_addmoney.htm');
    }
    //  用户提现
    public function put(){
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new cxModel();
        if($getdata['class'] == '5'){
            $getdata['addcont'] = '9';
            $usermoney = $cxmodel->where('uid',$this->cxbsuser['uid'])->where('addcont',$getdata['addcont'])->where('money_class','0')->order('id desc')->find();
        }
        if($getdata['class'] == '10'){
            $getdata['addcont'] = array('in','3,4');
            $usermoney = $cxmodel->where('uid',$this->cxbsuser['uid'])->where('addcont','in','3,4')->where('money_class','0')->order('id desc')->find();
        }
        //  查询可提现金额
        if(empty($usermoney) || $usermoney['user_money'] < '100'){
            $this->error("目前金额不够最低提现金额");
        }
        $usermoney = $usermoney->toArray();
        $usermoney['class'] = $getdata['class'];
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $validate = Loader::validate("UserMoney");
            if (!$validate->scene('addmoney')->check($data)) {
                $this->error($validate->getError());
            }
            if($data['money'] < '100'){
                $this->error("最低提现金额不得小于100元");
            }

            $newdata = array(
                'uid' => $this->cxbsuser['uid'],
                'money' => $data['money'],
                'addcont' => $getdata['class'],
                'cont' => '申请提现:课程课时费'.$data['money'].'元',
            );
            $user_money = $usermoney['user_money'] - $data['money'];
            if(model('UserMoney')->add_put($newdata)){
                $cxmodel->where('id',$usermoney['id'])->update(['user_money' => $user_money]);
                $this->success("申请提现成功,我们将在3个工作日内处理",'Index/map');
            }else{
                $this->error("申请提现失败，请重试！");
            }

        }
        //  构造提现参数
        $webs = array(
            'title' => '申请提现---'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'webs' => $webs,
            'usermoney' => $usermoney,
        ]);
        return view($this->temp.'user_putmoney.htm');
    }
    //  老师账单
    public function bill(){
        $cxmodel = new cxModel();
        $billlist = $cxmodel->where('uid',$this->cxbsuser['uid'])->where('money_class','0')->order('id desc')->paginate(20);

        //  构造提现参数
        $webs = array(
            'title' => '申请提现---'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'webs' => $webs,
            'list' => $billlist,
        ]);
        return view($this->temp.'user_bill.htm');
    }
// 生成订单编号
    protected function addoid(){
        $numbers = range (1,50);
        shuffle ($numbers);
        $num=8;
        $result = array_slice($numbers,0,$num);
        $result = implode('',$result);
        $newname = '';
        for ( $i = 0; $i < 8; $i++ ) {
            $newname .= substr($result, mt_rand(0, strlen($result) - 1), 1);
        }
        return $newname;
    }
}
